package com.ruoyi.product.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ruoyi.baseData.domain.Material;
import com.ruoyi.baseData.domain.Process;
import com.ruoyi.baseData.domain.ProcessRel;
import com.ruoyi.baseData.domain.vo.ProcessRelVo;
import com.ruoyi.baseData.mapper.MaterialMapper;
import com.ruoyi.baseData.service.SerialNumberService;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.common.core.utils.sql.QueryHelp;
import com.ruoyi.common.security.utils.SecurityUtils;

import com.ruoyi.product.domain.WoSub;
import com.ruoyi.product.domain.WorkOrder;
import com.ruoyi.product.domain.dto.WorkOrderDto;
import com.ruoyi.product.domain.vo.WorkOrderVo;
import com.ruoyi.product.mapper.WorkOrderMapper;
import com.ruoyi.product.service.WorkOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 *
 */
@Service
public class WorkOrderServiceImpl implements WorkOrderService {
    @Resource
    WorkOrderMapper workOrderMapper;

    @Resource
    SerialNumberService serialNumberService;

    @Resource
    MaterialMapper materialMapper;

    @Override
    public Page<WorkOrderVo> getWorkOrderByPage(WorkOrderDto pageEntity) {
        MPJLambdaWrapper<WorkOrder> wrapper = new MPJLambdaWrapper<WorkOrder>()
                .selectAll(WorkOrder.class)//查询user表全部字段
                .selectAs( Material::getName, WorkOrderVo::getMaterialName)
                .selectAs( Material::getSpec, WorkOrderVo::getMaterialSpec)
                //记的测试阶段
                .eq(WorkOrder::getDelFlag, 0)
                .leftJoin(Material.class, Material::getCode, WorkOrder::getMaterialCode)
                .orderByDesc(WorkOrder::getCreateTime);
        wrapper = QueryHelp.getPredicateEx(wrapper, WorkOrder.class, pageEntity);
        Page<WorkOrderVo> userList = workOrderMapper.selectJoinPage(new Page<>(pageEntity.getCurrentPage(), pageEntity.getPageSize()),   WorkOrderVo.class, wrapper);
        return userList;
    }

    @Override
    public WorkOrderVo addWorkOrder(WorkOrderDto testDto) {
        String userName = SecurityUtils.getUsername();
        WorkOrderVo workOrderVo =  new WorkOrderVo();
        String sn = serialNumberService.getNextSnByCode("WO");

        MPJLambdaWrapper<WorkOrder> wrapper = new MPJLambdaWrapper<WorkOrder>()
                .eq(WorkOrder::getWo, testDto.getWo())
                .ne(WorkOrder::getDelFlag, 1);
        WorkOrder material = workOrderMapper.selectOne(wrapper);
        if(null != material) {
            BeanUtils.copyProperties(material, workOrderVo, new String[]{"id"});
            if(!testDto.getType().equals(material.getType())) {
                throw new GlobalException("该工单[" +testDto.getWo() + "] 已经存在是" + material.getType() + "类型!");
            } else {
                return workOrderVo;
            }
        }
        WorkOrder test2 = new WorkOrder();
        BeanUtils.copyProperties(testDto, test2, new String[]{"id"});
        test2.setBatchCode(sn);
        test2.setMaterialCode(testDto.getMaterialCode());
        test2.setQty(testDto.getQty());
        /**
         * 完成
         */
        test2.setStartTime(new Date());
        /**
         * 工单类型:所有工序都是第一道工序
         */
        test2.setProceduresNum(1);
//        test2.setType(testDto.getType());
//        if("分切".equals(testDto.getType())) {
//            test2.setProceduresNum(3); //第三道工序
//        } else if("模切".equals(testDto.getType())) {
//            test2.setProceduresNum(1);
//            test2.setProceduresName("预混");
//        } else {
//
//        }
        test2.setStatus("未开工");
        test2.setMaxCount(0);
        test2.setDelFlag(false);
        test2.setCreateBy(userName);
        test2.setCreateTime(new Date());
        workOrderMapper.insert(test2);

        //查询工序和设备

        BeanUtils.copyProperties(test2, workOrderVo);
        return workOrderVo;
    }

    @Override
    public void updateWorkOrder(WorkOrderDto testDto) {
        String userName = SecurityUtils.getUsername();
        MPJLambdaWrapper<WorkOrder> wrapper = new MPJLambdaWrapper<WorkOrder>()
                .eq(WorkOrder::getId, testDto.getId());
        WorkOrder material = workOrderMapper.selectOne(wrapper);
        if(null == material) {
            throw new GlobalException("该工单不存在");
        }
        material.setStartTime(testDto.getStartTime());
        material.setEndTime(testDto.getEndTime());
//        BeanUtils.copyProperties(testDto, material);
        material.setUpdateBy(userName);
        material.setUpdateTime(new Date());
        workOrderMapper.updateById(material);
    }

    @Override
    public void delWorkOrder(WorkOrderDto testDto) {
        WorkOrder material = workOrderMapper.selectById(testDto.getId());
        if(null == material) {
            throw new GlobalException("该Test不存在");
        }
        material.setDelFlag(true);
        workOrderMapper.updateById(material);
    }

    @Override
    public WorkOrderVo getWorkOrderByCode(WorkOrderDto workOrderDto) {
        WorkOrderVo workOrderVo = new WorkOrderVo();
        MPJLambdaWrapper<WorkOrder> wrapper = new MPJLambdaWrapper<WorkOrder>()
                .eq(WorkOrder::getWo, workOrderDto.getWo());
        WorkOrder workOrder = workOrderMapper.selectOne(wrapper);
        if(null == workOrder) {
            throw new GlobalException("该工单不存在");
        }

        MPJLambdaWrapper<Material> wrapper2 = new MPJLambdaWrapper<Material>()
                .eq(Material::getCode, workOrder.getMaterialCode())
                .ne(Material::getDelFlag, 1);
        Material material = materialMapper.selectOne(wrapper2);
        BeanUtils.copyProperties(workOrder, workOrderVo);
        workOrderVo.setMaterialName(material.getName());
        workOrderVo.setMaterialSpec(material.getSpec());
        return workOrderVo;
    }
}




